<template>
	<view class="container">
		<view class="address-list" v-if="addressList.length">
			<view class="a-address" v-for="(item,index) in addressList" :key="index">
				<view class="left-text" @click.stop="selectTap(item.id)">
					<view class="" v-if="item.isDefault">
						<u-tag text="默认" type="primary" size="mini" />
					</view>
					<view class="name-tel margin-top-xs">
						{{item.name}} {{item.phone}}
					</view>
					<view class="address-box">
						{{item.area}}{{item.address}}
					</view>
					<!-- <view class="margin-top-xs">
						<u-button @click.prevent="IsEnReceiving(item.id)" size="mini" :type="item.isDefault?'error':'primary'">{{item.isDefault?'取消默认':'设为默认'}}</u-button>
					</view> -->
				</view>
				<view class="right-edit" @tap="editAddess(item)"></view>
			</view>
		</view>
		<u-empty text="暂无数据" iconSize="200" mode="order" class="flex justify-center align-center" v-else>
			<u-button slot="bottom" size="medium" type="primary" text="确定" style="margin:0 30upx;"
				@click="pathUrl" v-if="!token">
				点击登录
			</u-button>
		</u-empty>
		<view class="bottom-box flex justify-between align-center" @click="editAddess" v-if="token">
			<view class="add-btn" @tap="addAddess">新增收货地址</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [],
				loading:false,
				token:uni.getStorageSync("token")
			}
		},
		onShow() {
			if(this.token){
				this.getData()
			}
		},
		onPullDownRefresh(){
			this.getData()
		},
		methods: {
			selectTap(id) {
				this.addressList.forEach(v=>{
					if(v.id==id){
						v.isDefault=true
					}else {
						v.isDefault=false
					}
				})
				let data=this.addressList.filter(v=>v.isDefault)
				if(data){
					this.$apis.lastPage().address = data;
					console.log(this.$apis.lastPage().address);
					this.$apis.lastPage().addressId=data[0].id
					uni.navigateBack();
				}
				
			},
			getData(){
				this.$ajax.getgeceiving({MemberId:uni.getStorageSync('userInfo').id}).then(res=>{
					this.addressList=res.data
					uni.stopPullDownRefresh()
				})
			},
			editAddess(val) {
				uni.navigateTo({
					url:`/pages/home/addressEdit/addressEdit?data=${JSON.stringify(val)}`
				})
			},
			addAddess() {
				console.log("tap add new Address");
			},
			// 修改默认地址
			IsEnReceiving(Id){
				this.$ajax.IsEnReceiving({Id}).then(res=>{
					this.getData()
				})
			},
			pathUrl(){
				uni.reLaunch({
					url:'/pages/home/login/login'
				})
			}
		}
	}
</script>

<style>
	.container {
		background-color: #F2f2f2;
	}

	.address-list {
		width: 100%;
		background-color: #fff;
		margin-top: 20upx;
		padding-bottom: 100upx;
	}

	.address-list .a-address {
		width: 720upx;
		margin-left: 30upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30upx 0;
		border-bottom: 1upx solid #eee;
	}

	.a-address .left-text {
		width: 610upx;
		box-sizing: border-box;
		padding-left: 58upx;
	}

	.a-address .left-text.active {
		background: url() no-repeat left center;
		background-size: 28upx auto;
	}
	.a-address .left-text .name-tel {
		margin-bottom: 20upx;
	}

	.a-address .left-text .address-box {
		font-size: 24upx;
		color: #888888;
		line-height: 36upx;
	}

	.a-address .right-edit {
		width: 109upx;
		height: 100%;
		padding: 50upx 0 50upx 58upx;
		box-sizing: border-box;
		background: url() no-repeat 43upx center;
		background-size: 35upx auto;
	}
	
	.bottom-box {
		position: fixed;
		width: 100%;
		bottom: 0;
		font-size: 28upx;
		color: #000000;
		border-top: 1upx solid #eee;
		height: 120upx;
		line-height: 100upx;
		background: #fff url() no-repeat 705upx center;
		background-size: 15upx auto;
	}

	.bottom-box .add-btn {
		margin-left: 30upx;
		padding-left: 70upx;
		background: url() no-repeat left center;
		background-size: 40upx auto;
	}
</style>
